﻿namespace ChinaUnicom
{
    using ChinaUnicom.Models;
    using ChinaUnicom.Storage;
    using System.Collections.Generic;

    public sealed class Context
    {
        static object locker = new object();

        static Context context;

        public Context()
        {
            Account = new Account();
            Client = new Client();
            Package = new Package();
            Fee = new Fee();
            Point = new Point();
            FeeHistories = new FeeHistoryCollection();
        }

        public Account Account { get; set; }

        public Client Client { get; set; }

        public Point Point { get; set; }

        public Product Product { get; set; }

        public FeeHistoryCollection FeeHistories { get; set; }

        public Package Package { get; set; }

        public Fee Fee { get; set; }

        public void Reset()
        {
            Account = new Account();
            Client = new Client();
            Package = new Package();
            Fee = new Fee();
            Point = new Point();
            FeeHistories = new FeeHistoryCollection();
            Product = new Product();

            Store();
        }

        public static Context Current
        {
            get
            {
                if (context == null)
                {
                    lock (locker)
                    {
                        if (context == null)
                        {
                            context = JsonStorage.Restore<Context>();
                            context = (context != null ? context : new Context());
                        }
                    }
                }

                return context;
            }
        }

        public void Store()
        {
            JsonStorage.Store(this);
        }
    }
}
